package com.sad.service;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sad.dao.RecordManager;
import com.sad.model.JsonObject;
import com.sad.util.G;

@SuppressWarnings({ "rawtypes", "unchecked" })
public abstract class BaseService<T> implements IService<T> {

	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	@Override
	public List findList(Map<String, Object> parms) {
		return null;
	}

	@Override
	public T findById(Map<String, Object> id) {
		return null;
	}

	@Override
	public String create(Map<String, Object> parms) {
		String pageid = (String) parms.get(G.K.pageid);
		JsonObject jObject = (JsonObject) RecordManager.loadRecord(pageid).post(parms);

		return buildJson(jObject);
	}

	protected String buildJson(JsonObject jObject) {
		String json = "{\"success\":\"#value#\"}";
		if (jObject != null) {
			boolean success = jObject.isSuccess();
			json = json.replaceAll("#value#", success + "");

		} else {
			json = json.replaceAll("#value#", "false");
		}
		return json;
	}

	@Override
	public String edit(Map<String, Object> parms) {
		return null;
	}

	@Override
	public boolean delete(Map<String, Object> parms) {
		return false;
	}

}
